<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0px;
        padding: 0px;
      }
      .box1 {
        width: 100px;
        height: 100px;
        background: red;
      }
      .box2 {
        width: 100px;
        height: 100px;
        background: yellow;
      }
      .box3 {
        width: 100px;
        height: 100px;
        background: green;
      }
    </style>
  </head>
  <body>
    <button>按钮一</button>
    <button>按钮二</button>
    <button>按钮三</button>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
  </body>
</html>
<script>
  var btns = document.querySelectorAll("button");
  var divs = document.querySelectorAll("div");
  btns.forEach(function (item, key) {
    // console.log(item);
    item.onclick = function () {
      // console.log(key);
      // 把所有的div隐藏起来；
      divs.forEach(function (val) {
        val.style.display = "none";
      });
      // 把点击按钮键名和div键名相同的div显示出来
      // console.log(key);
      divs[key].style.display = "block";

      // 给当前点击的按钮背景颜色；
      // console.log(btns[key]);
      // console.log(this);
      // 去掉所有按钮的背景颜色
      btns.forEach(function (btn) {
        btn.style.background = "";
      });

      this.style.background = "red";
    };
  });

  // for(let i=0;i<btns.length;i++){
  //     btns[i].onclick = function(){
  //         console.log(i);
  //     }
  // }

  // for(let i=0;i<3;i++){
  //     for(let i=0;i<3;i++){
  //         console.log(i);
  //     }
  // }
</script>
